package com.cucn.ingmall.cart.vo;

import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;

/**
 * 整个购物车
 * 需要计算的属性要重写get方法,保证每次获取属性都会进行计算
 *
 * @author KouChaoJie
 * @since: 2022/1/13 17:09
 */
public class Cart {
    /**
     * 所有购物项
     */
    private List<CartItem> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    /**
     * 购物车所有商品总价
     */
    private BigDecimal totalAmount;
    /**
     * 优惠价格
     */
    private BigDecimal reduce = new BigDecimal("0");

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    /**
     * 计算商品总数量
     */
    public Integer getCountNum() {
        int count = 0;
        if (!items.isEmpty()) {
            for (CartItem item : items) {
                count = count + item.getCount();
            }
        }
        return count;
    }

    /**
     * 计算商品类型数量
     */
    public Integer getCountType() {
        int count = 0;
        if (!items.isEmpty()) {
            for (CartItem item : items) {
                count++;
            }
        }
        return count;
    }

    /**
     * 计算商品的总金额
     */
    public BigDecimal getTotalAmount() {
        BigDecimal p = new BigDecimal("0");
        if (!CollectionUtils.isEmpty(items)) {
            for (CartItem item : items) {
                if (item.getCheck()){
                    p = p.add(item.getTotalPrice());
                }
            }
        }
        p = p.subtract(getReduce());
        return p;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
